// pages/knowledge/documentDetail/documentDetail.js
import Dialog from '@vant/weapp/dialog/dialog';
const dayjs = require('dayjs')

const {imgUrl} = getApp().globalData
import {knowledgeApi} from '@/utils/request/api/knowledge'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: imgUrl + 'knowledge/',
        tgablesInit: [
            {
                name: "标准号",
                value: "",
                prop:""
            },
            {
                name: "标准状态",
                value: "",
                prop:"is_effect"
            },
            {
                name: "中文标准名称",
                value: "",
                prop:"name"
            },
            {
                name: "发布日期",
                value: "",
                prop:"created_at"
            },
            {
                name: "发布单位",
                value: "",
                prop:""
            },
            {
                name: "实施单位",
                value: "",
                prop:""
            }
        ],
        tableList: [],
        tablesShow: false,
        dialogShow: false,
        documentId: "",
        documentArr: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.id) {
            this.setData({
                documentId: options.id
            })
            this.queryDocumentPage()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.tablesShowFun();
        this.queryDocumentPage()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    tablesShowFun() {
        let tables = []
        if (this.data.tablesShow == true) {
            this.data.tgablesInit.forEach((item, index) => {
                if (index < 3) {
                    tables.push(item)
                }
            })
        } else {
            this.data.tgablesInit.forEach(item => {
                tables.push(item)
            })
        }
        this.setData({
            tablesShow: !this.data.tablesShow,
            tableList: tables
        })
        console.log(`tableList`,this.data.tableList)
    },
    async collectBind(){
        let res = await knowledgeApi.postLawPolicyUrl(`collect`, {pipost_id: this.data.documentId})
    },
    downFiles() {
        this.setData({
            dialogShow: true
        })
    },
    async getUserInfo() {
        let res = await knowledgeApi.postDocumentUrl(`download`, {document_id: this.data.documentId})
        // this.setData({
        //     dialogShow: false
        // })
    },
    onClose() {
        this.setData({
            dialogShow: false
        })
    },
    async queryDocumentPage() {
        let res = await knowledgeApi.documentUrl('info', {id: this.data.documentId})
        let tables = this.data.tableList
        tables.map(item=>{
            console.log(`table循环`,item,res.data[item.prop])
            item.value = res.data[item.prop]
            return item
        })

        console.log(`·this.data.tableList`,this.data.tableList)
        res.data.updated_at = dayjs(res.data.updated_at).format('YYYY-MM-DD HH:DD')
        if (res.err_code == 0) {
            this.setData({
                documentArr: res.data,
                tableList:tables
            })
        }
    }
})